<?php

namespace App\Http\Controllers\Admin;

use App\Http\Forms\CategoryForm;
use App\Modules\Shop\Models\Category;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CategoryController extends Controller
{
    public function index(Request $request)
    {
        //更新分类中层级对应关系
//        $c = Category::query()->where([['pid', 0], ['parent', '<>', '顶级分类']])->get();
//        foreach ($c as $v) {
//            $cc = Category::query()->where("name", $v->parent)->first();
//            Category::query()->where("id", $v->id)->update(["pid" => $cc->id, "parent" => $cc->name]);
//        }
//        dd("同步数据库结束");
        $this->authorize('good-top_category');
        $name = $request->input('name', '');
        $data = Category::query()
            ->when(!empty($name), function ($query) use ($name) {
                return $query->where('name', 'like', '%' . $name . "%");
            })
            ->where('pid', 0)
            ->where([["name","!=","婚纱摄影"],["parent","!=","婚纱摄影"]])
            ->orderBy('sort', 'desc')
            ->orderBy('created_at', 'desc')
            ->paginate(20);
        // $topCategory = DB::select('SELECT * FROM categories WHERE PID =0 and name != "婚纱摄影" and parent != "婚纱摄影" AND DELETED_AT IS NULL ORDER BY SORT DESC');
        $aIndexFixedCate = ['婚纱摄影','结婚首饰','家用电器','美妆护肤','礼服租售','床上用品','家俱家私','时尚服饰','婚宴喜酒'];//首页固定分类
        return view('category.index', compact('data', 'parent', 'topCategory', 'name', 'aIndexFixedCate'));
    }

    public function create(Request $request)
    {
        $this->authorize('top_category-add');
        $parent = $request->input('parent');
        $form = $this->form(CategoryForm::class, [
            'url' => route('admin.category.store'),
            'method' => 'POST',
            'model' => ['parent' => $parent],
        ]);
        return view('category.form', compact('form'));
    }

    public function store()
    {
        $this->authorize('top_category-add');
        $form = $this->form(CategoryForm::class);
        if (!$form->isValid()) {
            return redirect()->back()->withErrors($form->getErrors())->withInput();
        }
        $data = $form->getFieldValues();
        $data['parent'] = "顶级分类";
        $data['pid'] = 0;
        Category::query()->create($data);
        return redirect()->route('admin.category.index');
    }

    public function edit(Category $category, Request $request)
    {
        $this->authorize('top_category-edit');
        $parent = $request->input('parent');
        $form = $this->form(CategoryForm::class, [
            'url' => route('admin.category.update', ['category' => $category]),
            'method' => 'PUT',
            'model' => $category
        ]);
        return view('category.form', compact('form'));
    }

    public function update(Category $category, Request $request)
    {
        $this->authorize('top_category-edit');
        $parent = $request->input('parent');
        $form = $this->form(CategoryForm::class);
        if (!$form->isValid()) {
            return redirect()->back()->withErrors($form->getErrors())->withInput();
        }
        $data = $form->getFieldValues();
        
        $data['parent'] = '顶级分类';
        $data['pid'] = 0;
        $category->update($data);
        return redirect()->route('admin.category.index', ['parent' => $parent]);
    }

    public function destroy(Category $category)
    {
        $this->authorize('top_category-delete');
        $category->delete();
        return redirect()->back();
    }

    public function selectCategory(Request $request)
    {
        $parent = $request->input('parent');
        $re = Category::query()
            ->where('pid', $parent)
            ->get()
            ->pluck('name', 'id');
        return $re;
    }

    public function changeSort(Request $request)
    {
        $id = $request->input('id');
        $sort = $request->input('sort');
        Category::query()
            ->where('id', $id)
            ->update(['sort' => $sort]);
        return redirect()->back();
    }
}
